﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Dispatcher;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace LotteryWCF.Behavior
{
    public class DispatchMessageInspector : IDispatchMessageInspector
    {
        public static string LastReceivedHeader;

        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
            MessageHeaders messageHeadersElement = OperationContext.Current.IncomingMessageHeaders;
            Int32 id = messageHeadersElement.FindHeader("username", "") + messageHeadersElement.FindHeader("password", "");

            if (id > -1)
            {
                String username = messageHeadersElement.GetHeader<String>("username", "");
                String password = messageHeadersElement.GetHeader<String>("password", "");
                if (username == "7692AA70EEF92B42" && password == "00E19868F0C755F0")
                {
                    LastReceivedHeader = username;
                    return null;
                }
            }
            throw new Exception();
        }

        public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
        }
    }
}